﻿using System;
using System.Collections.Generic;
using Thuoc24h.Business;
using Thuoc24h.Common;
using Thuoc24h.Data;
using System.Linq;
using Utils = CSS.Utils;

namespace Thuoc24h.Home.Layouts
{
    public partial class Home : BasePage.BaseMasterPages
    {
        private int _yahoo = 2;
        private int _skype = 1;
        private int _phone = 3;
        private int _positionBanner = 1;
        private int _positionRight = 2;

        protected void Page_Load(object sender, EventArgs e)
        {

            //   user = (Customer)Session["Customer"];
            LoadCart();
            if (!Page.IsPostBack)
            {
                SupportOnline();
                Ads();
                BuildNewsMenu();
                MedicineMenuLtr.Text = BuilMedicineMenu(null);
            }
            if (CustomerUtils.IsLogin())
            {
                LogedInPLH.Visible = true;
                NotLogedInPLH.Visible = false;
                if (Session["Customer"] != null)
                {
                    var user = (Customer)Session["Customer"];
                    ltrUserName.Text = user.Name;

                }

            }
            else
            {
                LogedInPLH.Visible = false;
                NotLogedInPLH.Visible = true;
            }
        }
        private void BuildNewsMenu()
        {
            var categories = context.Categories.Where(it => it.CategoryType == 2).OrderBy(it => it.SortOrder);
            NewsMenuRpt.DataSource = categories;
            NewsMenuRpt.DataBind();
        }
        private string BuilMedicineMenu(int? parentId)
        {
            var htmlStr = "";
            var categories = context.Categories.Where(it => it.CategoryType == 1).OrderBy(it => it.SortOrder).ToList();
            if (categories != null)
            {

                htmlStr += "<ul>";
                foreach (var category in categories)
                {
                    if (category.ParentId == parentId)
                    {
                        htmlStr += string.Format("<li><a href=\"/{0}\">{1}</a>{2}</li>",
                                                string.Format("{0}/{1}", "tim-thuoc",
                                                              Utils.BuildUrl(category.UnsignName, category.Id)),
                                                category.Name, BuilMedicineMenu(category.Id));
                    }
                }
                htmlStr += "</ul>";
            }
            return htmlStr;
        }
        private void SupportOnline()
        {
            var str = "";
            var listYahoo = ReviewRepository.GetSupportsByType(_yahoo);
            foreach (var item in listYahoo)
            {
                str += "<a title=\"" + item.FullName + "\" href=\"ymsgr:sendim?" + item.Data + "\"><img border=\"0\" alt=\"" + item.FullName + "\" src=\"http://opi.yahoo.com/online?u=" + item.Data + "&amp;m=g&amp;t=2&amp;l=us" + "\"></a>";

            }
            ltrYahooSupport.Text = str;

            string strskype = "";
            var listSkype = ReviewRepository.GetSupportsByType(_skype);
            foreach (var item in listSkype)
            {

                strskype += "<a title=\"" + item.FullName + "\" href=\"skype:" + item.Data + "?chat\"><img src=\"/Themes/Images/skype.png" + "\"/></a>";
            }

            ltrSkypeSupport.Text = strskype;
            string strPhone = "";
            var listPhone = ReviewRepository.GetSupportsByType(_phone);
            foreach (var item in listPhone)
            {

                strPhone += "<span>" + item.Data + "</span>";
            }

            PhoneLtr.Text = strPhone;
        }

        private void Ads()
        {
            string str = "";
            foreach (var ad in ReviewRepository.GetAdsBanner(_positionBanner))
            {
                str += "<a href=\"" + ad.Link + "\" ><img src=\"" + ad.ImageUrl + "\" width=\"400px\" height=\"75px\"/></a>";
            }
            ltrAdsImage.Text = str;

            //-----------------------------------
            string strRight = "";
            foreach (var ad in ReviewRepository.GetAdsBanner(_positionRight))
            {
                strRight += "<li><a href=\"" + ad.Link + "\"> " + ad.EmbedCode + "</a> </li>";
            }
            ltrAdsRgith.Text = strRight;

        }

        private void LoadCart()
        {
            int _count = 0;
            if (Session["Order"] != null)
            {
                var orders = (List<ShopCartEntity>)Session["Order"];

                foreach (var order in orders)
                {
                    _count += order.Quantity;

                }


            }
            ltrCartCount.Text = _count.ToString();
        }

    }
}